#include <iostream>
#include <Eigen/Dense>
#include <opencv2/core.hpp>
#include <opencv2/core/eigen.hpp>
#include <opencv2/opencv.hpp>

int main() {
    // Load image
    cv::Mat image = cv::imread("../pic.png", cv::IMREAD_GRAYSCALE);
    if (image.empty()) {
        std::cerr << "Failed to load image!" << std::endl;
        return -1;
    }

    // Show original grayscale image
    cv::imshow("Original GrayScale Image", image);

    // Convert OpenCV Mat to Eigen Matrix (double precision)
    Eigen::MatrixXd eigenMat_pic;
    cv::cv2eigen(image, eigenMat_pic);

    // Normalize the Eigen Matrix to range [0, 255]
    double minVal = eigenMat_pic.minCoeff();
    double maxVal = eigenMat_pic.maxCoeff();
    eigenMat_pic = (eigenMat_pic.array() - minVal) * (255.0 / (maxVal - minVal));

    // Convert Eigen Matrix to OpenCV Mat (CV_8U type)
    cv::Mat displayMat;
    cv::eigen2cv(eigenMat_pic, displayMat);
    displayMat.convertTo(displayMat, CV_8U);

    // Display converted image
    cv::imshow("Converted Image from Eigen Matrix", displayMat);

    cv::waitKey(0);
    return 0;
}
